package com.lams.service.archive.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.struts2.ServletActionContext;

import com.lams.jdbc.QueryUtils;
import com.lams.service.BaseFacade;
import com.lams.service.archive.IRepairFacade;
import com.lams.util.DateHelper;
import com.lams.util.IdFactory;
import com.lams.util.ResultEntity;
import com.opensymphony.xwork2.ActionContext;

public class RepairFacade extends BaseFacade implements IRepairFacade {
	/**
	 * 查询报修SQL编码
	 */
	private String querySql;
	/**
	 * 添加报修SQL编码
	 */
	private String addSql;
	/**
	 * 更新报修SQL
	 */
	private String updateSqlX;
	/**
	 * 删除报修SQL
	 */
	private String deleteSql;
	/**
	 * 自动分配维护人员SQL
	 */
	private String querySqlStaff;
	/**
	 * 故障范围SQL
	 */
	private String faultConfinesSql;
	
	// private IBaseDAO dao;
	//
	// public IBaseDAO getDao() {
	// return dao;
	// }
	//
	// public void setDao(IBaseDAO dao) {
	// this.dao = dao;
	// }
	@Override
	public ResultEntity sessionRepair(){ 
		Map sessionName = (Map)ActionContext.getContext().getSession().get("user");
		List sessionRole = (List)ActionContext.getContext().getSession().get("role");
		sessionName.put("IP", ServletActionContext.getRequest().getRemoteAddr());
		List data = new ArrayList();
		data.add(0,sessionName);
		//System.out.println("######################################################################"+sessionRole);
		ResultEntity result = new ResultEntity();
		result.setData(data);
		result.setSuccess(true);
		return result;
	}
	
	
	@Override
	public ResultEntity queryRepair(Map params, int start, int limit) {
//		Map map = (Map)ActionContext.getContext().getSession().get("user");
//		System.out.println("####################################################################"+(map.get("USER_NAME"))+"####################################################################");  
		
		String sql = QueryUtils.getSqlBySqlCode(this.getQuerySql(), params);
		int totalCount = QueryUtils.getTotalCount(sql);
		List data = QueryUtils.executeQuery(start, limit, sql);
		//System.out.println("######################################################################"+data1);
		ResultEntity result = new ResultEntity();
		result.setData(data);
		result.setTotalCount(totalCount);
		result.setSuccess(true);
		return result;
	}
	
	
	@Override
	public ResultEntity queryRepairStaff(Map params, int start, int limit) {
//		Map map = (Map)ActionContext.getContext().getSession().get("user");
//		System.out.println("####################################################################"+(map.get("USER_NAME"))+"####################################################################");  
		
		String sql = QueryUtils.getSqlBySqlCode(this.getQuerySqlStaff(), params);
		int totalCount = QueryUtils.getTotalCount(sql);
		List data = QueryUtils.executeQuery(0, 1, sql);
		//System.out.println("######################################################################"+data1);
		ResultEntity result = new ResultEntity();
		result.setData(data);
		result.setTotalCount(totalCount);
		result.setSuccess(true);
		return result;
	}

	@Override
	public ResultEntity addRepair(Map params, String type) {
		String time = DateHelper.formatYMDHMS(new Date());
		String ssql;
		ResultEntity result = new ResultEntity();
		if("add".equals(type) || "update".equals(type)){
			if("add".equals(type)){
//				String sqlx = QueryUtils.getSqlBySqlCode(this.getQuerySqlStaff(), params);			
//				Map	data = QueryUtils.executeRecord(sqlx);
				params.put("REPAIR_ID", IdFactory.getNextNav("REPAIR_LIST", "REPAIR_ID"));
//				params.put("REPAIR_USER_ID", (String)map.get("USER_ID"));
//				params.put("REPAIR_USER_NAME", name);
				params.put("REPAIR_CLASS", "高三四班");
//				params.put("FAULT_PRINCIPAL", (String)data.get("MAINTAIN_STAFF_NAME"));
//				params.put("FAULT_PRINCIPAL_ID", data.get("MAINTAIN_STAFF_ID"));
				params.put("REPAIR_TIME", time);
				params.put("FAULT_STATUS", "0");
				params.put("DISPOSE_TIME", "等待处理");
				params.put("REPAIR_IP", ServletActionContext.getRequest().getRemoteAddr());
				params.put("CONDUCT_PROCESS", "报修("+time+")");
				params.put("SHOWW", "1");
			}else if ("update".equals(type)){
//				String sqlxQ = QueryUtils.getSqlBySqlCode(this.getFaultConfinesSql(), params);
//				Map dataq =  QueryUtils.executeRecord(sqlxQ);
//				String fault_confines2 = (String)dataq.get("FAULT_CONFINES");
//				String fault_confines = (String)params.get("FAULT_CONFINES");
//				System.out.println("#############################################################"+fault_confines);
//				System.out.println("#############################################################"+(String)params.get("FAULT_PRINCIPAL_ID"));
//				//判断报修状态是否改变
//				if (!fault_confines.equals(fault_confines2)){
//					//重新查找负责人
//				String sqlx = QueryUtils.getSqlBySqlCode(this.getQuerySqlStaff(), params);
//				Map data = QueryUtils.executeRecord(sqlx);
//				String fault_principal = (String)data.get("MAINTAIN_STAFF_NAME");
//				params.put("FAULT_PRINCIPAL", fault_principal);
//				}
				//判断转报或接报
				String processing_actions = (String)params.get("PROCESSING_ACTIONS");
				String estimate = (String)params.get("ESTIMATE");
//				System.out.println("#############################################################vvvv"+estimate);
				Map map = (Map)ActionContext.getContext().getSession().get("user");
				String name = (String)map.get("USER_NAME");
				String conduct_process1 = (String)params.get("CONDUCT_PROCESS");
				if (!processing_actions.equals("-1")){
					String processing_memo = (String)params.get("PROCESSING_MEMO") ;
					
					if (processing_actions.equals("0")){
						String conduct_process = conduct_process1 +"-->接报<"+name+">("+time+"){"+ processing_memo+"}" ;
						params.put("CONDUCT_PROCESS", conduct_process);
						params.put("DISPOSE_TIME", time);
						params.put("FAULT_STATUS", "2");
					}else if (processing_actions.equals("1")){
						String conduct_process = conduct_process1 +"-->转报<"+name+">("+time+"){"+ processing_memo +"}"; 
						params.put("CONDUCT_PROCESS", conduct_process);
						params.put("FAULT_STATUS", "0");
					}
				}else if (estimate!=null){
					String estimate_memo = (String)params.get("ESTIMATE_MEMO") ;
					String conduct_process = conduct_process1 +"-->维修完毕『"+estimate+"』<"+name+">("+time+"){"+ estimate_memo +"}"; 
					params.put("CONDUCT_PROCESS", conduct_process);
					params.put("FAULT_STATUS", "1");
					//System.out.println("#############################################################"+estimate);
				}
			}
			ssql = QueryUtils.getSqlBySqlCode("add".equals(type)?this.getAddSql():this.getUpdateSqlX(), params);
			//cssql = QueryUtils.getSqlBySqlCode("add".equals(type)?this.getInsertCSSql():this.getUpdateCSSql(), params);
			//String[] sqls = {ssql,cssql};
			//String[] sqls = {ssql};
			//System.out.println("####################################################################"+ssql+"####################################################################");
			int flag = 0;
			try {
				flag = super.getDao().executeUpdate(ssql);
				result.setSuccess(true);
				result.setMessage("执行操作成功");
			} catch (SQLException e) {
				e.printStackTrace();
				result.setSuccess(true);
				result.setMessage("执行操作失败");
			}
		} else{
			result.setSuccess(false);
			result.setMessage("更新类型错误");
		}
		return result;
	}
	
	
	@Override
	public ResultEntity deleteRepair(Map params, String type) {
		String ssql,cssql;
		ResultEntity result = new ResultEntity();
		if("delete".equals(type)){
			if("delete".equals(type)){
				params.put("SHOWW", "0");
			}
			ssql = QueryUtils.getSqlBySqlCode(this.getDeleteSql(), params);
			//System.out.println("####################################################################"+ssql+"####################################################################");
			int flag = 0;
			try {
				flag = super.getDao().executeUpdate(ssql);
				result.setSuccess(true);
				result.setMessage("执行删除操作成功");
			} catch (SQLException e) {
				e.printStackTrace();
				result.setSuccess(true);
				result.setMessage("执行删除操作失败");
			}
		} else{
			result.setSuccess(false);
			result.setMessage("更新类型错误");
		}
		return result;
	}
	
	
	

	public String getQuerySql() {
		return querySql;
	}

	public void setQuerySql(String querySql) {
		this.querySql = querySql;
	}

	public String getAddSql() {
		return addSql;
	}

	public void setAddSql(String addSql) {
		this.addSql = addSql;
	}

	public String getDeleteSql() {
		return deleteSql;
	}

	public void setDeleteSql(String deleteSql) {
		this.deleteSql = deleteSql;
	}

	public String getUpdateSqlX() {
		return updateSqlX;
	}

	public void setUpdateSqlX(String updateSqlX) {
		this.updateSqlX = updateSqlX;
	}

	public String getQuerySqlStaff() {
		return querySqlStaff;
	}

	public void setQuerySqlStaff(String querySqlStaff) {
		this.querySqlStaff = querySqlStaff;
	}


	public String getFaultConfinesSql() {
		return faultConfinesSql;
	}

	public void setFaultConfinesSql(String faultConfinesSql) {
		this.faultConfinesSql = faultConfinesSql;
	}
	
	

}
